package sctu.edu.java.year2016.team03.maoyuhao.lianxi.L3;

/**
 * Created by cc on 2016/6/7.
 */
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;

    public class SleepMethod1 extends JFrame {
        private Thread t;
        private static Color[] color = { Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK,Color.blue, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.PINK};
        private static final Random rand = new Random();
        private static Color getC(){
            return color[rand.nextInt(color.length)];
        }
        public SleepMethod1(){
            t = new Thread(new Runnable() {
                int x = 300;
                int y = 500;
                @Override
                public void run() {
                    while(true){
                        try{
                            t.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Graphics graphics = getGraphics();
                        graphics.setColor(getC());
                        graphics.drawLine(x,y,400,y++);
                        if(y>=80){
                            y=50;
                        }
                    }
                }
            });
            t.start();
        }

        public static void main(String[] args) {
            init(new SleepMethod1(), 100, 100);
        }
        public static void init(JFrame frame,int width,int height){
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(width, height);
            frame.setVisible(true);
        }
    }


